﻿using MiniExcelLibs.Attributes;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO.Ports;
using System.Threading;

namespace JinYuan.Models
{
    /// <summary>
    /// 通信设备实体类
    /// </summary>
    public class PlcDevice
    {
        /// <summary>
        /// PLC编号
        /// </summary>
        public int PlcNum { get; set; }
        /// <summary>
        /// IP地址
        /// </summary>
        public string IPAddress { get; set; }
        /// <summary>
        /// 端口
        /// </summary>
        public int Port { get; set; }

        /// <summary>
        /// PLC类型
        /// </summary>
        public string PLCType { get; set; }
        /// <summary>
        /// 是否启用
        /// </summary>
        public bool IsActive { get; set; }
        /// <summary>
        /// 心跳地址
        /// </summary>
        public string HeartBeat { get; set; }
        /// <summary>
        /// 是否启用心跳消息
        /// </summary>
        public bool IsHeartBeat { get; set; }
        /// <summary>
        /// 链接成功标志
        /// </summary>
        public bool IsConnected { get; set; }
        /// <summary>
        /// 是否是首次连接
        /// </summary> 
        public bool IsFirstConnect { get; set; } = true;

        /// <summary>
        /// 重连周期
        /// </summary>

        public int ReConnectTime { get; set; } = 2000;

        /// <summary>
        /// 休眠时间
        /// </summary>

        public int SleepTime { get; set; } = 10;

        /// <summary>
        /// 大小端
        /// </summary>
        [JsonIgnore]
        public int DataFormat { get; set; } = 0;
        /// <summary>
        /// 出错次数
        /// </summary>
        public int ErrorTimes { get; set; }

        /// <summary>
        /// 容错次数
        /// </summary>
        public int AllowErrorTimes { get; set; } = 3;

        /// <summary>
        /// 多线程取消源标志位
        /// </summary>
        public CancellationTokenSource Cts { get; set; }
        /// <summary>
        /// 通信组集合
        /// </summary>
        public List<PlcGroup> GroupList { get; set; } = new List<PlcGroup>();
    }


    public class PlcGroup
    {
        /// <summary>
        /// PLC编号
        /// </summary>
        public int PlcNum { get; set; }
        /// <summary>
        ///通信组名称
        /// </summary>
        public string GroupName { get; set; }
        /// <summary>
        /// 起始地址
        /// </summary>
        public string Start { get; set; }
        /// <summary>
        /// 长度
        /// </summary>
        public ushort Length { get; set; }
        /// <summary>
        /// 是否为触发读取
        /// </summary>
        public bool IsTiggerRead { get; set; }

        /// <summary>
        /// 是否需要反馈结果到PLC
        /// </summary>
        public bool IsFeedbackPlc { get; set; }

        /// <summary>
        /// 是否启用
        /// </summary>
        public bool IsActive { get; set; }
        /// <summary>
        /// 备注说明
        /// </summary>
        public string Remark { get; set; }
        /// <summary>
        /// 变量集合
        /// </summary>
        [ExcelIgnore]
        public List<PlcVariable> VarList { get; set; }
    }

    /// <summary>
    /// 变量属性类
    /// </summary>
    public class PlcVariable
    {
        /// <summary>
        /// PLC编号
        /// </summary>
        public int PlcNum { get; set; }
        /// <summary>
        /// 变量名称
        /// </summary>
        public string VarName { get; set; }

        /// <summary>
        /// 变量地址
        /// </summary>
        public string VarAddress { get; set; }
        /// <summary>
        /// 起始索引
        /// </summary>
        public string Start { get; set; }
        /// <summary>
        /// 数据类型
        /// </summary>
        public string DataType { get; set; }
        /// <summary>
        /// 偏移或长度
        /// </summary>
        public int OffsetOrLength { get; set; }
        /// <summary>
        /// 所属通信组名称
        /// </summary>
        public string GroupName { get; set; }
    }


    public class PlcStates
    {
        // ... 现有的属性 ...
        public short OldStatus { get; set; }
        public int StatusCount { get; set; }
        public string GuidAlarm { get; set; }
        public List<bool> Status { get; set; }
    }

    // 串口配置类
    public class SerialPortConfig
    {
        public bool IsActive { get; set; }
        public string PortName { get; set; }
        public int BaudRate { get; set; }
        public string DeviceName { get; set; }
        public SerialPort serialPort { get; set; }
        public bool IsConnected { get; set; }
    }
}
